<?php
require_once("admin_data_base_functions.php");
$config_file = parse_ini_file("../../properties/config.ini");
$include_path = "../";
include_once("../util/db_manager.php");
include_once("../util/log_writer.php");
$lw = LogWriter::getInstance("../../properties/log4php-level2.properties");
$error = false;
$msg = "";
	//validate
	$lw->debug("validating news");
	if (strlen($_POST["news"]) == 0) {
		$error = true;
		$msg = "Enter news. ";
	$lw->debug("news validation failed");
	}
	
	if (!$error) {
		try {
			//start db transaction
		$lw->debug("getting db manager");
			$dbManager = DBManager::getInstance($config_file);
		$lw->debug("getting db connection");
			$conn = $dbManager->fetchConnection();
			$query = "INSERT INTO news(news, news_url, news_type) VALUES('". $_POST["news"] . "',";
			$url = $_POST["url"];
			if (strlen($_POST["url"]) == 0) {
				$query = $query . "null,'";
			} else {
				$query = $query . "'" . urldecode($_POST["url"]) . "','";
			}
			$query = $query . $_POST["news-type"] . "')";
		$lw->debug("inserting news '". $query . "'");
			$dbManager->processQuery($query,$conn);
		$lw->debug("releasing db conn");
			$dbManager->releaseConnection($conn);
			$msg = "completed";
		} catch (Exception $e) {
			$error = true;
			$msg = $e->getMessage();
		$lw->error($e);
		} 
	}
	$lw->debug("generating response");
	$response = getResponseStart($error, $msg, 0);
	$response = $response . getResponseEnd();
	$lw->debug("processing news response");
	include("admin_data_reponse.php");
	$lw->debug("processed news response");
?>
